package com.self.av;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class DrawView extends View {
    private final Paint paint = new Paint();
    private final Path path = new Path();
    private final List<Path> paths = new ArrayList<>();
    private boolean isDrawingEnabled = false; // 控制是否允许绘制

    // ===== 新增：绘制模式枚举 =====
    public enum DrawingMode {
        PAINT,  // 画笔模式
        LINE    // 直线模式（默认）
    }
    private DrawingMode currentMode = DrawingMode.LINE; // 当前模式

    // ===== 新增：直线模式相关变量 =====
    private float startX = -1; // 直线起点 X
    private float startY = -1; // 直线起点 Y
    private final Path tempLinePath = new Path(); // 直线预览路径

    public DrawView(Context context) {
        super(context);
        init();
    }

    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(5f);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (Path p : paths) {
            canvas.drawPath(p, paint);
        }
        canvas.drawPath(path, paint);
        // ===== 新增：绘制直线模式的预览路径 =====
        canvas.drawPath(tempLinePath, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isDrawingEnabled) {
            return false; // 不处理触摸事件，让事件传递到下层视图
        }
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (currentMode == DrawingMode.PAINT) {
                    path.moveTo(x, y);
                } else {
                    // 直线模式：记录起点，初始化预览路径
                    startX = x;
                    startY = y;
                    tempLinePath.reset();
                    tempLinePath.moveTo(startX, startY);
                }
                return true;
            case MotionEvent.ACTION_MOVE:
                if (currentMode == DrawingMode.PAINT) {
                    path.lineTo(x, y);
                } else if (startX != -1) {
                    // 直线模式：更新预览路径（从起点到当前触摸点）
                    tempLinePath.reset();
                    tempLinePath.moveTo(startX, startY);
                    tempLinePath.lineTo(x, y);
                }
                break;
            case MotionEvent.ACTION_UP:
                if (currentMode == DrawingMode.PAINT) {
                    paths.add(new Path(path));
                    path.reset();
                } else if (startX != -1) {
                    // 直线模式：保存最终直线路径，重置临时状态
                    Path linePath = new Path();
                    linePath.moveTo(startX, startY);
                    linePath.lineTo(x, y);
                    paths.add(linePath);
                    tempLinePath.reset();
                    startX = -1; // 重置起点标记
                    startY = -1;
                }
                break;
            default:
                return false;
        }
        invalidate();
        return true;
    }

    public void clear() {
        paths.clear();
        path.reset();
        tempLinePath.reset();
        startX = -1;
        startY = -1;
        invalidate();
    }

    public void setDrawingMode(DrawingMode mode) {
        this.currentMode = mode;
        // 切换模式时重置临时状态
        if (mode != DrawingMode.LINE) {
            tempLinePath.reset();
            startX = -1;
            startY = -1;
        }
        invalidate();
    }

    // ===== 新增：获取当前模式（供外部调用）=====
    public DrawingMode getCurrentMode() {
        return currentMode;
    }

    // 设置是否允许绘制
    public void setDrawingEnabled(boolean enabled) {
        this.isDrawingEnabled = enabled;
    }

    public boolean isDrawingEnabled() {
        return isDrawingEnabled;
    }

}